package cn.shop.merchant.controller.gametemplate;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.model.gameconfig.GatewaySecret;
import cn.shop.merchant.req.GatewaySecretQueryReq;
import cn.shop.merchant.service.GatewaySecretService;
import cn.shop.utils.ApiResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 通讯秘钥接口
 */
@RestController
@RequestMapping("/gateway/secret")
public class GatewaySecretController {

    @Resource
    private GatewaySecretService secretService;

    /**
     * 获取秘钥列表
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/")
    public ApiResult<Map<String, Object>> list(GatewaySecretQueryReq req) {
        long uid = StpUtil.getSession().getLong(UID);
        Map<String, Object> map = secretService.list(uid, req);
        return ApiResult.success(map);
    }

    /**
     * 更新秘钥
     *
     * @param id 编号
     * @return 返回结果
     */
    @PutMapping("/{id}")
    public ApiResult<Map<String, Object>> update(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        GatewaySecret secret = secretService.get(id, uid);
        if (secret == null) {
            return ApiResult.fail();
        }
        return ApiResult.result(secretService.updateSecret(secret));
    }

    /**
     * 删除秘钥
     *
     * @param id 编号
     * @return 返回结果
     */
    @DeleteMapping("/{id}")
    public ApiResult<Map<String, Object>> delete(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        return ApiResult.result(secretService.deleteSecret(id, uid));
    }

}
